import Vue from 'vue'
// 管理购物车数据
const state={
	cartList:[
		{
			isSelected:true,
			count:2,
			"promId": 0,
			"showPoints": false,
			"itemTagList": [
			{
			"itemId": 1535004,
			"tagId": 128111157,
			"freshmanExclusive": false,
			"name": "暖冬特惠",
			"subType": 204,
			"forbidJump": false,
			"type": 2
			}
			],
			"rank": 1,
			"id": 1535004,
			"sellVolume": 4001,
			"primaryPicUrl": "https://yanxuan-item.nosdn.127.net/f79906f1b1fe86420ea40473de66ec0e.png",
			"soldOut": false,
			"sortFlag": 0,
			"commentCount": 0,
			"onSaleTime": 1538101761748,
			"picMode": 1,
			"commentWithPicCount": 0,
			"underShelf": false,
			"status": 2,
			"couponConflict": true,
			"forbiddenBuy": false,
			"promotionDesc": "暖冬特惠",
			"limitedFlag": 204,
			"pieceNum": 0,
			"itemSizeTableDetailFlag": false,
			"forbidExclusiveCal": false,
			"rewardShareFlag": false,
			"updateTime": 1575893634989,
			"showCommentEntrance": true,
			"pieceUnitDesc": "件",
			"specialPromTag": "",
			"counterPrice": 299,
			"categoryL2Id": 0,
			"retailPrice": 209,
			"primarySkuPreSellPrice": 0,
			"preLimitFlag": 0,
			"itemPromValid": true,
			"promTag": "暖冬特惠",
			"source": 0,
			"points": 0,
			"primarySkuPreSellStatus": 0,
			"extraServiceFlag": 0,
			"flashPageLink": "",
			"autoOnsaleTimeLeft": 0,
			"innerData": {},
			"saleCenterSkuId": 0,
			"pointsStatus": 0,
			"extraPrice": "",
			"colorNum": 0,
			"showTime": 0,
			"autoOnsaleTime": 0,
			"preemptionStatus": 1,
			"isPreemption": 0,
			"zcSearchFlag": false,
			"name": "男式色拉姆内衣套装2.0",
			"appExclusiveFlag": false,
			"itemType": 1,
			"listPicUrl": "https://yanxuan-item.nosdn.127.net/c2eeb1b872af1b8efc179a7515aacdaa.png",
			"pointsPrice": 0,
			"simpleDesc": "色拉姆发热面料，加厚升级",
			"seoTitle": "",
			"newItemFlag": false,
			"buttonType": 0,
			"primarySkuId": 1636062,
			"displaySkuId": 1636056,
			"productPlace": "",
			"itemSizeTableFlag": false
		},
		{
			isSelected:true,
			count:2,
			"promId": 0,
			"showPoints": false,
			"itemTagList": [
			{
			"itemId": 3810008,
			"tagId": 128111155,
			"freshmanExclusive": false,
			"name": "暖冬特惠",
			"subType": 204,
			"forbidJump": false,
			"type": 2
			}
			],
			"rank": 1,
			"id": 3810008,
			"sellVolume": 434,
			"primaryPicUrl": "https://yanxuan-item.nosdn.127.net/8ca5893efee1a4e33c915d6b6b44c107.png",
			"soldOut": false,
			"sortFlag": 0,
			"commentCount": 0,
			"onSaleTime": 1573444571736,
			"picMode": 1,
			"commentWithPicCount": 0,
			"underShelf": false,
			"status": 2,
			"couponConflict": true,
			"forbiddenBuy": false,
			"promotionDesc": "暖冬特惠",
			"limitedFlag": 204,
			"pieceNum": 0,
			"itemSizeTableDetailFlag": false,
			"forbidExclusiveCal": false,
			"rewardShareFlag": false,
			"updateTime": 1576029274173,
			"showCommentEntrance": true,
			"pieceUnitDesc": "件",
			"specialPromTag": "",
			"counterPrice": 1299,
			"categoryL2Id": 0,
			"retailPrice": 1039,
			"primarySkuPreSellPrice": 0,
			"preLimitFlag": 0,
			"itemPromValid": true,
			"promTag": "暖冬特惠",
			"source": 0,
			"points": 0,
			"primarySkuPreSellStatus": 0,
			"extraServiceFlag": 0,
			"flashPageLink": "",
			"autoOnsaleTimeLeft": 0,
			"innerData": {},
			"saleCenterSkuId": 0,
			"pointsStatus": 0,
			"extraPrice": "",
			"colorNum": 3,
			"showTime": 0,
			"autoOnsaleTime": 0,
			"preemptionStatus": 1,
			"isPreemption": 0,
			"zcSearchFlag": false,
			"name": "女式户外派克羽绒服",
			"appExclusiveFlag": false,
			"itemType": 1,
			"listPicUrl": "https://yanxuan-item.nosdn.127.net/b73d07da373d3e4b16d2431b501aad99.png",
			"pointsPrice": 0,
			"simpleDesc": "强势牛津布，雨雪无惧",
			"seoTitle": "",
			"newItemFlag": false,
			"buttonType": 0,
			"primarySkuId": 300186026,
			"displaySkuId": 300186028,
			"productPlace": "",
			"itemSizeTableFlag": false
		}
	]
}

const actions={
	
}

const mutations={
	// 添加商品数据至cartList
	addShopItemMutation(state,shopItem){
		/*
		思路：
			1. 购物车已有该商品，在原有的商品count上累加
			2. 如果购物车没有该商品，直接添加该商品至购物车
		*/
	 let shopObj=state.cartList.find(item=>item.id===shopItem.id)
	 if(shopObj){//购物车已有该商品
		 shopObj.count+=1
	 }else{//购物车没有该商品
			// 初始化数量为1
			// 非响应式数据
		 // shopItem.count=1
		 // 设置添加商品为选中
		 // shopItem.isSelected=true
		 
		 // 响应式数据
		 Vue.set(shopItem,'count',1)
		 Vue.set(shopItem,"isSelected",true)
		 state.cartList.push(shopItem)
	 }
	},
	
	// 修改购物车中你商品数量
	changeCountMutation(state,{isAdd,index}){
		if(isAdd){
			state.cartList[index].count+=1
		}else{
			// 判断商品数量是否大于一
			if(state.cartList[index].count>1){
				state.cartList[index].count-=1
			}else{
				wx.showModal({
					content:'你确认删除该商品吗',
					success:(res)=>{
						if(res.confirm){
							// 删除该商品
							state.cartList.splice(index,1)
						}
					}
				})
			}
		}
	},
	
	// 修改是否选中状态
	changeSelectedMutation(state,{isSelected,index}){
		state.cartList[index].isSelected=isSelected
	},
	// 全选全不选
	changeAllSelectedMutation(state,isAllSelected){
		state.cartList.forEach(item=>item.isSelected=isAllSelected)
	}
}

const getters={
	// 判断是否所有的商品都全部选中
	isAllSelected(state){
		return state.cartList.every(item=>item.isSelected)
	},
	// 修改总数量
	totalCount(){
		return state.cartList.reduce((pre,shopItem)=>{
			// 商品状态为选中的时候才累加
			return pre+=shopItem.isSelected?shopItem.count:0
		},0)
	},
	// 修改总价格
	totalPrice(){
		return state.cartList.reduce((pre,shopItem)=>{
			return pre+=shopItem.isSelected?shopItem.count*shopItem.retailPrice:0
		},0)
	}
}
export default{
	state,
	actions,
	mutations,
	getters
}